export const HOOKS = [
  'beforeCreate',
  'created',
  'beforeMount',
  'mounted',
  'beforeUpdate',
  'updated',
  'beforeDestroy',
  'destroyed'
];

// 策略模式
let starts = {};
// 合并data
starts.data = function (parentVal, childVal) {
  return childVal;
}

// // 合并computed
// starts.computed = function () {}
// // 合并watch
// starts.watch = function () {}
// // 合并methods
// starts.methods = function () {}

// 合并生命周期
HOOKS.forEach((hooks) => {
  starts[hooks] = mergeHook;
})

function mergeHook (parentVal, childVal) {
  if (childVal) {
    if (parentVal) {
      return parentVal.concat(childVal);
    } else {
      return [childVal];
    }
  } else {
    return parentVal;
  }
}

export function mergeOptions (parent, child) {
  // console.log('parent===', parent)
  // console.log('child===', child)

  const options = {};

  for (let key in parent) {
    mergeField(key)
  }

  for (let key in child) {
    mergeField(key)
  }

  function mergeField (key) {
    // 根据key 策略模式
    if (starts[key]) {
      options[key] = starts[key](parent[key], child[key])
    } else {
      options[key] = child[key]
    }
  }

  // console.log('options==', options)
  return options;

}
